<template>
  <el-container>
    <el-header class="homeHeader">
      <div class="title">云e办</div>
      <div>
        <el-button
            @click="goChat"
            size="normal"
            style="color: black;margin-right: 8px"
            icon="el-icon-bell"
            type="text">
        </el-button>
        <el-dropdown class="userInfo" @command="commandHandler">
          <span class="el-dropdown-link">
            {{ user.name }}
            <!--<i><img :src="user.userFace"></i>-->
            <el-avatar shape="square" :size="40" :src="user.userFace"></el-avatar>
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="userInfo">个人中心</el-dropdown-item>
            <el-dropdown-item command="setting">设置</el-dropdown-item>
            <el-dropdown-item command="logout">注销登录</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </el-header>

    <el-container>
      <el-aside width="200px">
        <el-menu router unique-opened>
          <template v-for="(item,index) in routes">
            <el-submenu :index="index+''" v-if="!item.hidden" :key="index">
              <template slot="title">
                <i :class="item.iconCls" style="color: #409eff;margin-right: 5px"></i> {{ item.name }}
              </template>
              <el-menu-item :index="children.path" v-for="(children,indexj) in item.children" :key="indexj">
                {{ children.name }}
              </el-menu-item>
            </el-submenu>
          </template>
        </el-menu>
      </el-aside>

      <el-main>
        <el-breadcrumb v-if="this.$router.currentRoute.path !== '/home'">
          <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item> {{ this.$router.currentRoute.name }}</el-breadcrumb-item>
        </el-breadcrumb>
        <div v-else class="homeWelcome">欢迎来到云e办系统</div>
        <router-view class="homeRouterView"/>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: "HomeView",
  data() {
    return {
      user: this.$store.state.currentAdmin
    }
  },
  methods: {
    goChat() {
      this.$router.push('/chat')
    },
    // 下拉菜单操作
    commandHandler(command) {
      if (command === 'logout') {
        this.$confirm('确定注销登录吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // 注销登录操作
          this.postRequest('/logout');
          // 清空菜单
          this.$store.commit('INIT_ROUTES', []);
          // 清空token和用户信息
          window.sessionStorage.removeItem('tokenStr')
          window.sessionStorage.removeItem('user')
          //跳转到登录页
          this.$router.replace('/')
        }).catch(() => {
          this.$message({
            type: 'warning',
            message: '已取消注销操作！'
          });
        })
      }
      if (command === 'userInfo') {
        this.$router.push('/userInfo')
      }
    }
  },
  computed: {
    routes() {
      return this.$store.state.routes;
    }
  }
}
</script>

<style scoped>
.homeHeader {
  display: flex;
  background: #409eff;
  align-items: center;
  justify-content: space-between;
  padding: 0 15px;
  box-sizing: border-box;
}

.homeHeader .title {
  font-family: 华文楷体;
  font-size: 25px;
  color: white;
}

.homeHeader .userInfo {
  cursor: pointer;
}

.el-dropdown-link img {
  width: 40px;
  height: 40px;
  border-radius: 24px;
  margin-left: 8px;
}

.homeWelcome {
  font-size: 30px;
  font-family: 华文楷体;
  color: #409eff;
  text-align: center;
  padding-top: 50px;
}

.homeRouterView {
  padding-top: 20px;
}
</style>
